package com.plant.action;

import com.lowagie.text.Image;
import com.plant.entity.PgcArticle;
import com.plant.entity.PgcTopic;
import com.plant.entity.UgcUser;
import com.plant.service.FindService;
import com.plant.tool.DateUtils;
import com.plant.tool.MyImage;

import java.awt.image.BufferedImage;
import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 *涉及到的表：表1：pgc_article 表2：pgc_upload_images 表3：ugc_topic_join；
 */

@Controller                                                                     
@RequestMapping(value = "/discover")                                                
public class DiscoverAction {
	@Resource(name = "findServiceImpl")//注解扫描接口，接口FindService
	private FindService findService;


	@RequestMapping(value = "list")  //---1.查看官方的话题显示
	@ResponseBody                                                               
	public Map<Object,Object> topics(HttpServletResponse response,PgcTopic pg,Integer page,String time,HttpServletRequest request,String secret,String mdsc) {
		response.setHeader("access-control-allow-origin","*");	
		Map<Object, Object> map1=new HashMap<Object, Object>();
		List<Object> list=new ArrayList<Object>();
		List<Object[]> list1=new ArrayList<Object[]>();
		List<Object[]> list2=new ArrayList<Object[]>();
		if(page==null||time==null||secret==null||mdsc==null){
			map1.put("state", 0);
			map1.put("message", "参数不能为空");
			return map1;
		}else{
			Md md=new Md();
			String mdc=md.md5calc(secret+"flower_duigu2016");//md5加密匹配
			if(mdsc.endsWith(mdc)){
				MyImage im=new MyImage();
				list1=findService.listall(page,time);
				list2=findService.listall(page+1,time);
				if(list2.size()==0){
					map1.put("hasmore", 0);
				}else{
					map1.put("hasmore", 1);
				}
				for (int i = 0; i < list1.size(); i++) {
					Map<Object, Object> map=new HashMap<Object, Object>();
					Object[] obj=list1.get(i);
					String imurl=(String) obj[3];
					map.put("topicid", obj[0]);
					map.put("uid", obj[1]);
					UgcUser u=new UgcUser();
					u.setId((Integer)obj[1]);
					UgcUser user=(UgcUser) findService.topicuserinf(u);
					map.put("nickname", user.getNickname());
					map.put("photo", user.getPhoto());
					map.put("title", obj[2]);
					try {
						URL url = new URL(imurl);
						URLConnection uc = url.openConnection();
						@SuppressWarnings("unused")
						InputStream in = uc.getInputStream();
						BufferedImage image=im.getBufferedImage(imurl);
						map.put("height", image.getHeight()); //--获取图片的高度
						map.put("width", image.getWidth());   //--获取图片的宽度
					} catch (Exception e) {
						map.put("height", ""); //--获取图片的高度
						map.put("width", "");   //--获取图片的宽度
					}
					map.put("url", obj[3]);
					map.put("heat", obj[4]);
					map.put("pv", obj[5]);
					map.put("contentnum", findService.alltopiccount(pg, obj[0]));
					list.add(map);
				}
				map1.put("list", list);
				map1.put("state", 1);
				map1.put("message", "查询成功");
				return map1;
			}else{
				map1.put("state", 0);
				map1.put("message", "秘钥错误");
				return map1;
			}
		}
	}



	@RequestMapping(value="article")//---2.查看显示官方文章
	@ResponseBody
	public Map<Object,Object> pgcArticle(HttpServletResponse response,PgcArticle pa,UgcUser user,Integer page,
			String time,HttpServletRequest request,String secret,String mdsc){
		response.setHeader("access-control-allow-origin","*");
		Map<Object, Object> map1=new HashMap<Object, Object>();
		if(secret==null||mdsc==null||page==null||time==null){
			map1.put("message", "参数不能为空");
			map1.put("state", 0);
			return map1;
		}else{
			Md md=new Md();
			String mdc=md.md5calc(secret+"flower_duigu2016");
			if(mdsc.endsWith(mdc)){
				List<Map<String, Object>> list=new ArrayList<Map<String,Object>>();
				List<Map<String, Object>> list2=new ArrayList<Map<String,Object>>();
				list=findService.listArticle(page, time);	//1.查看显示官方文章
				list2=findService.listArticle(page+1, time);//1.查看显示官方文章
				if(list2.size()>0){
					map1.put("hasmore", 1);
				}else{
					map1.put("hasmore", 0);
				}
				SimpleDateFormat sim=new SimpleDateFormat("yyyy/MM/dd");
				for (Map<String, Object> map : list) {
					String image=map.get("image").toString();
					try {
						Image img = Image.getInstance(new URL(image));
						map.put("height", img.getHeight()); //--获取图片的高度
						map.put("width", img.getWidth());   //--获取图片的宽度
					} catch (Exception e) {
					}
					String stime=map.get("add_time").toString();
					map.put("add_time", sim.format(DateUtils.StaticgetUnixStamps(stime)));
				}
				map1.put("article", list);
				map1.put("message", "查询推荐文章列表成功");
				map1.put("state", 1);
				return map1;
			}else{
				map1.put("message", "秘钥错误");
				map1.put("state", 0);
				return map1;
			}
		}
	} 



	@RequestMapping(value="onearticle")
	@ResponseBody
	public Map<Object, Object> oneArticle(HttpServletResponse response,PgcArticle pa,UgcUser user,Integer page,String time,String secret,String mdsc){ //--3.查看某个文章的详细内容以及评论回复
		response.setHeader("access-control-allow-origin","*");
		List<Object> list=new ArrayList<Object>();
		Map<Object, Object> map=new HashMap<Object, Object>();
		if(pa.getId()==null||secret==null||mdsc==null||page==null||time==null){
			map.put("error", "输入参数为空");
			return map;
		}else{
			Md md=new Md();
			String mdc=md.md5calc(secret+"flower_duigu2016");
			if(mdsc.endsWith(mdc)){
				List<Object[]> list1=new ArrayList<Object[]>();
				List<Object[]> list2=new ArrayList<Object[]>();
				//		    		pa.setId(1);
				//		    		String times="2016-09-2 16:22:34";
				list1=findService.listoneArticle(pa);
				for (int i = 0; i < list1.size(); i++) {
					Object[] obj=list1.get(i);
					map.put("articlcontent", obj[0]);
					String imurl=obj[1].toString();
					map.put("image", obj[1]);
					try {
						Image img = Image.getInstance(new URL(imurl));  
						map.put("image_height", img.getHeight()); //--获取图片的高度
						map.put("image_width", img.getWidth());   //--获取图片的宽度
					} catch (Exception e) {
						map.put("image_height", ""); //--获取图片的高度
						map.put("image_width", "");   //--获取图片的宽度
					}
				}
				list2=findService.oneArtcontent(pa, page, time);
				for (int i = 0; i < list2.size(); i++) {
					Map<Object, Object> map2=new HashMap<Object, Object>();
					Object[] obj=list2.get(i);
					map2.put("ucontent", obj[0]);
					map2.put("uid", obj[1]);
					map2.put("nickname", obj[2]);
					map2.put("photo", obj[3]);
					list.add(map2);
				}
				map.put("ucount", list);
				return map;
			}else{
				map.put("error", "秘钥错误");
				return map;
			}
		}
	}



	@RequestMapping(value="oneatopic")//---单个话题详情
	@ResponseBody
	public Map<Object, Object> OneaTopic(Integer topic_id,PgcTopic pt,HttpServletResponse response,UgcUser user,String secret,String pust_val){ //--3.查看某个文章的详细内容以及评论回复
		response.setHeader("access-control-allow-origin","*");
		Map<Object, Object> map=new HashMap<Object, Object>();
		if(secret==null||pust_val==null||topic_id==null){
			map.put("message", "输入参数为空");
			map.put("state", 0);
			return map;
		}else{
			Md md=new Md();
			String mdc=md.md5calc(pust_val+"flower_duigu2016");
			if(secret.endsWith(mdc)){
				pt.setId(topic_id);
				List<Object[]> list1=new ArrayList<Object[]>();
				list1=findService.listoneTopic(pt);
				if(list1.size()==0){
					map.put("", "话题详情为空");
				}else{
					PgcTopic ptt=(PgcTopic) findService.ObjectTopic(pt);
					for (int i = 0; i < list1.size(); i++) {
						Object[] obj=list1.get(i);
						map.put("id_key", obj[0]);
						map.put("title", obj[1]);
						map.put("pv", obj[2]);
						Integer pv=Integer.parseInt(obj[2].toString())+1;
						ptt.setPv(pv);
						map.put("image", obj[3]);
						String imurl=obj[3].toString();
						try {
							Image img = Image.getInstance(new URL(imurl));  
							map.put("image_height", img.getHeight()); //--获取图片的高度
							map.put("image_width", img.getWidth());   //--获取图片的宽度
						} catch (Exception e) {
							map.put("image_height", ""); //--获取图片的高度
							map.put("image_width", "");   //--获取图片的宽度
						}
						map.put("content", obj[4]);
						map.put("user_id", obj[5]);
						map.put("user_nickname", obj[6]);
						map.put("user_photo", obj[7]);
						map.put("join_count", findService.JoinTopicCount(pt));
					}
					map.put("message","查询单个话题详情成功");
					map.put("state", 1);
					findService.UpdateTopicPv(ptt);
				}
				return map;
			}else{
				map.put("message", "秘钥错误");
				map.put("state", 0);
				return map;
			}
		}
	}


	@RequestMapping(value="topic_join_list")
	@ResponseBody
	public Map<Object, Object> TopicJoinlist(UgcUser u,Integer page,String first_page_time,Integer user_id,Integer topic_id,PgcTopic pt,HttpServletResponse response,UgcUser user,String secret,String pust_val){ //--3.查看某个话题的详细内容以及评论回复
		response.setHeader("access-control-allow-origin","*");
		List<Object> list=new ArrayList<Object>();
		Map<Object, Object> map1=new HashMap<Object, Object>();
		if(secret==null||pust_val==null||page==null||first_page_time==null||topic_id==null||user_id==null){
			map1.put("message", "输入参数为空");
			map1.put("state", 0);
			return map1;
		}else{
			Md md=new Md();
			String mdc=md.md5calc(pust_val+"flower_duigu2016");
			if(secret.endsWith(mdc)){
				List<Object[]> list1=new ArrayList<Object[]>();
				//		    			pt.setId(42);
				//		    			String time="2016-10-19 19:14:00";
				//		    			u.setId(147);
				u.setId(user_id);
				pt.setId(topic_id);
				SimpleDateFormat sim=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
				DateUtils du=new DateUtils();
				list1=findService.listTopicComment(pt, page, first_page_time);
				for (int i = 0; i < list1.size(); i++) {
					Map<Object, Object> map=new HashMap<Object, Object>();
					Object[] obj=list1.get(i);
					map.put("id_key", obj[0]);
					map.put("user_id", obj[1]);
					String t=sim.format(obj[2]);
					long l=du.getUnixStamps(t);
					map.put("time",du.TopicCommentTime(l));
					map.put("to_topic_id", obj[3]);
					map.put("content", obj[4]);
					map.put("user_nickname", obj[5]);
					map.put("user_photo", obj[6]);	
					long islike=findService.TopicCommentIslike(u,obj[0]);
					if(islike==0){
						map.put("is_like", 0);
					}else{
						map.put("is_like", 1);
					}
					map.put("like_count", findService.TopicCommentlikeCount(obj[0]));
					map.put("images_list", findService.listTopicCommentImage(obj[0]));
					list.add(map);
				}
				map1.put("list", list);
				map1.put("message","查询话题参与成功");
				map1.put("state", 1);
				return map1;
			}else{
				map1.put("message", "秘钥错误");
				map1.put("state", 0);
				return map1;
			}
		}
	}

	@RequestMapping(value="hot_topic_join_list")
	@ResponseBody
	public Map<Object, Object> HotTopicJoinList(UgcUser u,Integer page,String first_page_time,Integer user_id,Integer topic_id,PgcTopic pt,HttpServletResponse response,UgcUser user,String secret,String pust_val){ //--3.查看某个话题详细内容以及评论回复
		response.setHeader("access-control-allow-origin","*");
		List<Object> list=new ArrayList<Object>();
		Map<Object, Object> map1=new HashMap<Object, Object>();
		if(secret==null||pust_val==null||first_page_time==null||topic_id==null){
			map1.put("message", "输入参数为空");
			map1.put("state", 0);
			return map1;
		}else{
			Md md=new Md();
			String mdc=md.md5calc(pust_val+"flower_duigu2016");
			if(secret.endsWith(mdc)){
				List<Object[]> list1=new ArrayList<Object[]>();
				//				        pt.setId(35);
				//				        u.setId(144);
				//				    	String time="2016-10-20 19:14:00";
				u.setId(user_id);
				pt.setId(topic_id);
				SimpleDateFormat sim=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
				DateUtils du=new DateUtils();
				list1=findService.listHotTopicComment(pt, first_page_time);
				for (int i = 0; i < list1.size(); i++) {
					Map<Object, Object> map=new HashMap<Object, Object>();
					Object[] obj=list1.get(i);
					map.put("user_id", obj[0]);
					map.put("user_nickname", obj[1]);
					map.put("user_photo", obj[2]);
					map.put("id_key", obj[3]);
					map.put("content", obj[5]);
					map.put("to_topic_id", obj[6]);
					String t=sim.format(obj[7]);
					long l=du.getUnixStamps(t);
					map.put("time",du.TopicCommentTime(l));
					map.put("like_count", obj[8]);
					long islike=findService.TopicCommentIslike(u,obj[3]);
					if(islike==0){
						map.put("is_like", 0);
					}else{
						map.put("is_like", 1);
					}
					list.add(map);
				}
				map1.put("list", list);
				map1.put("message","查询单个话题详情成功");
				map1.put("state", 1);
				return map1;
			}else{
				map1.put("message", "秘钥错误");
				map1.put("state", 0);
				return map1;
			}
		}
	}

}
